app.service('couponsService',['$q','utilService','$http',function($q,utilService,$http){
    var _this_=this;
    _this_.coupons={
        status:0
    };

    //修改优惠卷状态
    _this_.updateCouponsStatus=function($scope){
        _this_.coupons.id=$scope.couponsId;

        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain+"coupons",
            data:_this_.coupons,
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.createQrcode = function($scope,id){
        console.info($scope.app.domain);
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'coupons/qrcode',
            data:{'id':id},
            method:'POST'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }

    this.downQrcode = function($scope,qrcodeName,couponsName){
        console.info($scope.app.domain);
        var deferred = $q.defer();
        $http({
            method:'post',
            url:$scope.app.domain + 'coupons/qrcode/download',
            data:{'qrcodeName':qrcodeName,'couponsName':couponsName},
            responseType: "arraybuffer",
            headers:{'Content-Type': 'application/x-www-form-urlencoded'},
            transformRequest: function(obj) {
                var str = [];
                for(var p in obj){
                    str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                }
                return str.join("&");
            }
        }).success(function(data){
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var fileName ="userConsume"+ (new Date()).getTime()+'.png';
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.download = fileName;
            a.href = URL.createObjectURL(blob);
            a.click();
        })
        return deferred.promise;
    }

    this.delCoupons = function($scope){
        var deferred = $q.defer();
        utilService.api({
            url:$scope.app.domain + 'all/coupons/del',
            data:{couponsId:$scope.param.couponsId},
            method:'PUT'
        }).then(function(result){
            deferred.resolve(result);
        });
        return deferred.promise;
    }
}])